package itman.elm.controller;

import itman.elm.po.Cart;
import itman.elm.service.CartService;
import itman.elm.service.impl.CartServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.util.List;

public class CartController {
    // http://localhost:8080/elm/CartController/listCart?   businessId=1&userId=17620073980
    public Object listCart(HttpServletRequest request, HttpServletResponse response) throws Exception{
        Cart cart = new Cart();
        if(request.getParameter("businessId")!=null) {
            cart.setBusinessId(Integer.parseInt(request.getParameter("businessId")));
        }
        if(request.getParameter("userId")!=null) {
            cart.setUserId(request.getParameter("userId"));
        }
        CartService service = new CartServiceImpl();
        List<Cart> cartResult = service.listCart(cart);
        return cartResult;
    }

    // http://localhost:8080/elm/CartController/saveCart?   foodId=1&businessId=1&userId=17620073980
    public Object saveCart(HttpServletRequest request,HttpServletResponse response) throws Exception{
        Cart cart = new Cart();
        cart.setFoodId(Integer.valueOf(request.getParameter("foodId")));
        cart.setBusinessId(Integer.valueOf(request.getParameter("businessId")));
        cart.setUserId(request.getParameter("userId"));
        CartService service = new CartServiceImpl();
        int result = service.saveCart(cart);
        return result;
    }

    // http://localhost:8080/elm/CartController/removeCart?   foodId=1&businessId=1&userId=17620073980
    public Object removeCart(HttpServletRequest request,HttpServletResponse response) throws Exception{
        Cart cart = new Cart();
        cart.setFoodId(Integer.valueOf(request.getParameter("foodId")));
        cart.setBusinessId(Integer.valueOf(request.getParameter("businessId")));
        cart.setUserId(request.getParameter("userId"));
        CartService service = new CartServiceImpl();
        int result = service.removeceCart(cart);
        return result;
    }

    // http://localhost:8080/elm/CartController/updateCart?   foodId=1&businessId=1&userId=17620073980&quantity=2
    public Object updateCart(HttpServletRequest request,HttpServletResponse response) throws Exception{
        Cart cart = new Cart();
        cart.setFoodId(Integer.valueOf(request.getParameter("foodId")));
        cart.setBusinessId(Integer.valueOf(request.getParameter("businessId")));
        cart.setUserId(request.getParameter("userId"));
        cart.setQuantity(Integer.valueOf(request.getParameter("quantity")));
        CartService service = new CartServiceImpl();
        int result = service.updateCart(cart);
        return result;
    }
}
